package com.kaibes.core.base.thread;

import java.util.concurrent.atomic.AtomicBoolean;

public interface TimingThread extends RunThread {

    long getTaskTime();
    void setTaskTime(long taskTime);
    long getInterval();
    void setInterval(long interval);
    long getCount();
    void setCount(long count);
    AtomicBoolean getFlag();
    
    default void run() {
        setCount(0);
        getFlag().set(true);
        long nowTime = System.currentTimeMillis();
        long taskTime = getTaskTime();
        while (getFlag().get()) {
            doOnInterval();
            try {
                Thread.sleep(getInterval());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            setCount(getCount()+1);
            long curTime = System.currentTimeMillis() - nowTime;
            if (curTime >= taskTime) {
                getFlag().set(false);
                doOnResult();
            }
        }
    }
    default void stop() {
        getFlag().set(false);
    }
    default void doOnInterval() {};
    default void doOnResult() {};
    
}
